package loader;

import static bogus.core.BogusCore.egl;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import owg.util.opengl.ColorF;
import world.Global;
import world.World;
import bogus.core.BogusCore;
import bogus.minigame.MinigameBase;
import graphics.Shape;
import hubol.Entity;
import hubol.HMinigame;

public class MiniLoader extends HMinigame{
    private World world;

    public MiniLoader(World world){
	super();
	System.out.println(HMinigame.me == this);
	this.world = world;
    }

    public void initImpl(){
    	new Loader();
    }

    private class Loader extends Entity{
	//private long goalTime = System.currentTimeMillis() + 2000; No need for functionality that simply wastes time until a release build
	private boolean rendered = false, loaded = false;

	//private World world; This field is unnecessary since we can implicitly access the world field from the outer class.

	private Constructor<?> instantiate;
	private MinigameBase instance;

	public Loader() {
	    super(0, 0);
	    //this.world = world;
	}

	public void step(){
	    if (rendered){
		if (!loaded){ //perform loading if necessary
		    Global.contentLoad(world.toString());
		    loaded = true;
		    instantiate = world.spawner.getConstructors()[0];
		    try {
			instance = (MinigameBase)instantiate.newInstance();
		    } catch (IllegalArgumentException e) {
			e.printStackTrace();
		    } catch (InstantiationException e) {
			e.printStackTrace();
		    } catch (IllegalAccessException e) {
			e.printStackTrace();
		    } catch (InvocationTargetException e) {
			e.printStackTrace();
		    }
		}
		//if (System.currentTimeMillis() >= goalTime){ //has the goal time been surpassed?
		destroy();
		BogusCore.loadingCompleted(instance);
		//}
	    }
	}

	public void render(){
	    egl.setColor(new ColorF(.25f, .53f, .64f, 1f));
	    Shape.drawRectangle(0, 0, 864, 486);
	    rendered = true;
	}

    }

	@Override
	public int getFps() {
		return 30;
	}

	@Override
	public void completeMinigameImpl() {
		// TODO Auto-generated method stub
		
	}

}
